package net.lesscoding.listener;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.validator.Msg;
import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

/**
 * @author eleven
 * @date 2024/5/30 21:59
 * @apiNote
 */
public class ICBCTransactionListener implements TransactionListener {

    /**
     * 回调操作
     * <br/>
     * 消息提交承购就会触发此方法的执行，用于完成本地事务
     *
     * @param message 消息
     * @param o       业务参数
     * @return LocalTransactionState
     */
    @Override
    public LocalTransactionState executeLocalTransaction(Message message, Object o) {
        System.out.println("预提交消息成功" + message);
        String tags = message.getTags();
        if (StrUtil.equals("TAG-A", tags)) {
            return LocalTransactionState.COMMIT_MESSAGE;
        }

        if (StrUtil.equals("TAG-B", tags)) {
            return LocalTransactionState.ROLLBACK_MESSAGE;
        }
        return LocalTransactionState.UNKNOW;
    }

    /**
     * 消息回查方法
     * <br/>
     * 常见的引发消息回查的原因有两个
     * 1. 回调操作返回了UNKNOW
     * 2. TC没有收到TM的最终全局事务确认指令
     *
     * @param messageExt 消息
     * @return LocalTransactionState
     */
    @Override
    public LocalTransactionState checkLocalTransaction(MessageExt messageExt) {
        System.out.println("执行消息回查" + messageExt);
        return LocalTransactionState.COMMIT_MESSAGE;
    }
}
